import gradio as gr
from dao.plt_fpga import PltFpga
from dao.control import Control
from deal_data import pre_function

if __name__ == "__main__":
    #初始化
    plt_fpga = PltFpga()
    control = Control(plt_fpga)

    with gr.Blocks(css="footer {visibility: hidden}") as webui:
        with gr.Tab("串口连接"):
            with gr.Row():
                with gr.Column():
                    available_serial_list = gr.Textbox(label="当前可用端口名字")
                with gr.Column():
                    btn_serial_refresh = gr.Button(value="串口刷新")
                    btn_serial_refresh.click(control.can_comm.list_available_ports, outputs=[available_serial_list])
            with gr.Row():
                with gr.Column():
                    fpga_serial_txt = gr.Textbox(label="FPGA端口",value="COM11")
                    fpge_baudrate_txt = gr.Textbox(label="FPGA波特率(9600)",value="9600")
                    fpge_bytesize_txt = gr.Textbox(label="FPGA字节大小(8)",value="8")
                    fpga_parity_txt = gr.Textbox(label="FPGA校验位NEO(N)",value="N")
                    fpga_stopbits_txt = gr.Textbox(label="FPGA停止位(1)",value="1")
                    btn_openFPGA_serial = gr.Button(value="打开FPGA串口")
                    btn_openFPGA_serial.click(fn=control.fun_btn_fpga_connect,
                                              inputs=[fpga_serial_txt,
                                                      fpge_baudrate_txt,
                                                      fpge_bytesize_txt,
                                                      fpga_parity_txt,
                                                      fpga_stopbits_txt])
                    btn_closeFPGA_serial = gr.Button(value="关闭FPGA串口")
                    btn_closeFPGA_serial.click(fn=control.fun_btn_fpga_disconnect)
                with gr.Column():
                    steer_serial_txt = gr.Textbox(label="位移台端口",value="COM8")
                    steer_baudrate_txt = gr.Textbox(label="位移台波特率(2000000)",value="2000000")
                    steer_bytesize_txt = gr.Textbox(label="位移台字节大小(8)",value="8")
                    steer_parity_txt = gr.Textbox(label="位移台校验位NEO(N)",value="N")
                    steer_stopbits_txt = gr.Textbox(label="位移台停止位(1)",value="1")
                    btn_openControl_serial = gr.Button(value="打开位移台串口")
                    btn_openControl_serial.click(fn=control.fun_btn_canbus_connect,
                                                 inputs=[steer_serial_txt,
                                                         steer_baudrate_txt,
                                                         steer_bytesize_txt,
                                                         steer_parity_txt,
                                                         steer_stopbits_txt])
                    btn_closeControl_serial = gr.Button(value="关闭位移台串口")
                    btn_closeControl_serial.click(fn=control.fun_btn_canbus_disconnect)

        with gr.Tab("检测"):
            with gr.Row():#表格行
                plot = gr.LinePlot(show_label=False)
            with gr.Row():#控制按钮行
                with gr.Column():
                    btn_start = gr.Button(value="检测开始")
                    btn_start.click(control.fun_btn_run)
                with gr.Column():
                    btn_stop = gr.Button(value="停止")
                    btn_stop.click(control.fun_btn_run)
                with gr.Column():
                    btn_refresh = gr.Button(value="刷新表格")
                    btn_refresh.click(plt_fpga.get_gradio_plot,outputs=[plot])
                with gr.Column():
                    btn_del = gr.Button(value="清空表格")
                    btn_del.click(plt_fpga.del_fpgalist,outputs=[plot])
            with gr.Row():
                btn_save = gr.Button(value="保存数据")
                btn_save.click(plt_fpga.save_to_json)

        with gr.Tab("数据管理"):
            load_plot = gr.LinePlot(show_label=False)
            data_input = gr.FileExplorer(root='./data', file_count='single', ignore_glob="*.py",
                                        container=True, show_label=True, label='选择数据文件')
            btn_view_plot = gr.Button(value="显示图像")
            btn_view_plot.click(fn=plt_fpga.json_to_plot,inputs=data_input,outputs=[load_plot])
        with gr.Tab("预处理"):
            def process_file(uploaded_file, pre_fun):
                print(f"文件上传成功，绝对路径为：{uploaded_file}")
                print(f"选择预处理方式{pre_fun}")
                pre_fig = pre_function.pre_single_display(uploaded_file, pre_fun)
                # print(pre_fig)
                return pre_fig
            with gr.Row():
                with gr.Column():
                    pre_input = gr.FileExplorer(root='./data', file_count='single', ignore_glob="*.py",
                                                container=True, show_label=True, label='选择数据文件')
                with gr.Column():
                    pre_change = gr.Checkboxgroup(["SG滤波", "归一化", "基线校正", "异常值剔除"], label="预处理选择",
                                                  info="可多种预处理同时选择,最后将展现原数据和处理后的数据图片")
                    pre_run_buttun = gr.Button(value="预处理")
            with gr.Row():
                pre_plot = gr.LinePlot()
            pre_run_buttun.click(fn=process_file, inputs=[pre_input, pre_change], outputs=pre_plot)

        with gr.Tab("debug"):
            with gr.Column():
                txt_angle_set = gr.Textbox(label="位置角度")
            with gr.Column():
                btn_angle_set = gr.Button(value="设置角度")
                btn_angle_set.click(fn=control.fun_btn_angle_set,inputs=[txt_angle_set])
                btn_angle_get = gr.Button(value="获取角度")
                txt_rotate_result = gr.Textbox(label="当前角度", value=control.can_comm.cur_pos)
                btn_angle_get.click(fn=control.fun_btn_angle_get, outputs=[txt_rotate_result])
            with gr.Column():
                btn_fpga_once = gr.Button(value="fpga发送一次r")
                btn_fpga_once.click(fn=control.fun_btn_fpga_test)

    webui.queue().launch()

